package com.example.dishit.model.dbproxy.data;

import java.util.ArrayList;
import java.util.List;

import com.example.dishit.model.dish.DishData;

public class DishTranslator {
    /** DishData ==> Dish */
    public static Dish translate(final DishData dishData) {
        // RESTAURANT
        String restaurantName = dishData.getRestaurantName();
        restaurantName = (restaurantName != null && !restaurantName.isEmpty()) ? restaurantName
                                                                               : "";
        String streetAddress = dishData.getStreetAddress();
        streetAddress = (streetAddress != null && !streetAddress.isEmpty()) ? streetAddress
                                                                            : "";
        String city = dishData.getCity();
        city = (city != null && !city.isEmpty()) ? city
                                                 : "";
        String state = dishData.getState();
        state = (state != null && !state.isEmpty()) ? state
                                                    : "";
        final String zipcodeStr = dishData.getZipcode();
        int zipcode = 0;
        if(zipcodeStr != null && !zipcodeStr.isEmpty()) {
            try { zipcode = Integer.parseInt(zipcodeStr); }
            catch(NumberFormatException e) { e.printStackTrace(); }
        }
        final Restaurant restaurant = new Restaurant(restaurantName, streetAddress, city, state, zipcode);
        
        // REVIEW
        final List<String> comments = dishData.getComments();
        final String comment; 
        if(comments != null && !comments.isEmpty()) {
            comment = dishData.getComments().get(0);
        } else {
            comment = "";
        }
        final Review review = new Review(restaurant, comment, dishData.getRating());
        
        // DISH
        final String recommendedBy = dishData.getRecommendedBy();
        final String disher = (recommendedBy != null && !recommendedBy.isEmpty()) ? recommendedBy
                                                                                  : "";
        return new Dish(review, disher);
    }
    
    /** Dish ==> DishData */
    public static DishData translate(final Dish dish) {
        final Review review = dish.getReview();
        final Restaurant restaurant = review.getRestaurant();
        
        final List<String> comments = new ArrayList<String>();
        comments.add(review.getComments());
        
        final List<String> imageUrls = new ArrayList<String>();
        imageUrls.add(dish.getImageURL());
        
        return new DishData.Builder()
            .restaurantName(restaurant.getName())
            .streetAddress(restaurant.getStreet())
            .city(restaurant.getCity())
            .state(restaurant.getState())
            .zipcode(Integer.toString(restaurant.getZipCode()))
            .comments(comments)
            .rating(review.getRating())
            .recommendedBy(dish.getDisher())
            .imageUrls(imageUrls)
            .build();
    }
}
